﻿function LoadQualificationAutoComplete() {
    cacheQualificationData = new Array();
    qualificationCount = 0;

    $("a[id^='QualificationAdd']").live("click", null, function () {
        qualificationCount++;

        if (qualificationCount == 2)
            $("a[id='QualificationAdd']").hide();
        else
            $("a[id='QualificationAdd']").show();

        var txtTagsID = "QualificationText" + skillsCount;
        // var btnAddID = "add" + window.count;
        var btnDelID = "QualificationDel" + skillsCount;

        var ul = $("#divQualificationAdd");

        if (qualificationCount <= 2) {
            var li = $("<div class=\"col-md-12 add-extra-another-input\"><input id='" + txtTagsID + "'  type=\"text\" placeholder=\"Enter Category\" class=\"form-control\" />"
                  //   + "<button id='" + btnAddID + "' >Add</button>"
                     + "<button id='" + btnDelID + "' class=\"add-extra-another-button\" ><img width=\"16\" height=\"16\" src=\"/img/delete-cross.png\"></button>")
            ul.before(li);
        }
        return false;
    });

    $("button[id^='QualificationDel']").live("click", null, function () {
        var li = $(this).parent();
        li.remove();
        qualificationCount--;
        $("a[id='QualificationAdd']").show();
        return false;
    });

    $("input:text[id^='QualificationText']").live("focus.autocomplete", null, function () {
        $(this).autocomplete({
            source: function (request, response) {
                if (cacheQualificationData["Qualification"] == null) {
                    $.ajax({
                        type: "POST",
                        url: "HandlerSearchCV.ashx",                        
                        data: { 'search': 'qualification' },
                        dataType: "json",
                        success: function (data) {
                            data.sort();
                            cacheQualificationData["Qualification"] = data;

                            var re = $.ui.autocomplete.escapeRegex(request.term);
                            var matcher = new RegExp("^" + re, "i");
                            var a = $.grep(cacheQualificationData["Qualification"], function (item, index) {
                                return matcher.test(item);
                            })

                            response(a);
                        },

                        error: function (xhr, tStatus, err) {
                            if (xhr.status == "401") {
                                alert(xhr.status + " User Authorisation failed.Please login again.");
                                location.reload();
                            }
                        }
                    })
                } else {
                    var re = $.ui.autocomplete.escapeRegex(request.term);
                    var matcher = new RegExp("^" + re, "i");
                    var a = $.grep(cacheQualificationData["Qualification"], function (item, index) {
                        return matcher.test(item);
                    });
                    if (typeof a === 'undefined') {
                        response("");
                    }
                    else
                        response(a);
                }
               
                
            },
            select: function (event, ui) {
                if (ui.item) {

                    window.scrollTo(0, 0);
                    $("body").ajaxLoader();

                    var pageSize = $('#ctl00_MainContent_ddlPageSize option:selected').val().trim();
                    var jobTitleText = "";
                    $("#JobTitleSearch :text").each(function () {
                        jobTitleText += $(this).val() + "$";
                    });

                    var jobSeekerPlaceText = "";
                    $("#Place :text").each(function () {
                        jobSeekerPlaceText += $(this).val() + "$";
                    });

                    var jobCategoryText = "";
                    $("#Category :text").each(function () {
                        jobCategoryText += $(this).val() + "$";
                    });


                    var jobSeekerEmploymentType = "";
                    $("#EmploymentType :text").each(function () {
                        jobSeekerEmploymentType += $(this).val() + "$";
                    });


                    var jobSeekerQualification = "";
                    $("#Qualification :text").each(function () {
                        jobSeekerQualification += $(this).val() + "$";
                    });

                    var jobSeekerSkills = "";
                    $("#Skills :text").each(function () {
                        jobSeekerSkills += $(this).val() + "$";
                    });

                    var selectedUpdateTime = $('#ctl00_SearchContainer_ddlCvUpdate option:selected').text().trim();
                    var selectedExperience = $('#ctl00_SearchContainer_DropDownListExperience option:selected').text().trim();

                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: "SearchCV.aspx/getByQualification",
                        data: JSON.stringify({ 'search': ui.item.value, 'pageSize': pageSize, 'updateTime': selectedUpdateTime, 'experience': selectedExperience, 'jobTitle': jobTitleText, 'location': jobSeekerPlaceText, 'category': jobCategoryText, 'employmentType': jobSeekerEmploymentType, 'qualification': jobSeekerQualification, 'skills': jobSeekerSkills }),
                        datatype: "json",
                        success: function (data) {
                            PrintGrid(data, 1);
                            $("body").ajaxLoaderRemove().delay(3000);
                        },
                        error: function (xhr, tStatus, err) {
                            $("body").ajaxLoaderRemove().delay(3000);
                            if (xhr.status == "401") {
                                alert(xhr.status + " User Authorisation failed.Please login again.");
                                location.reload();
                            }
                        },
                        complete: function (xhr, status) {
                            var msg = xhr.responseText;

                        }


                    })
                }
            },

            minLength: 1,
            delay: 500

        });       
    });
}